// 判断网速 kb
export function measureBW(): Promise<number> {
    return new Promise((resolve, reject) => {
        let startTime = 0;
        let endTime = 0;
        let fileSize = 0;
        let xhr = new XMLHttpRequest();
        let measureTimerStatus = false;
        let measureTimer = setTimeout(() => {
            if (!measureTimerStatus) {
                resolve(50);
            };
        }, 5000);
        xhr.onreadystatechange = () =>{
            if(xhr.readyState === 2){
                startTime = Date.now();
            }
            if (xhr.readyState === 4 && xhr.status === 200) {
                endTime = Date.now();
                fileSize = xhr.responseText.length;
                // console.log(fileSize);
                let speed = fileSize  / ((endTime - startTime) / 1000) / 1024;
                // console.log('measureBW res:', speed);
                measureTimerStatus = true;
                measureTimer && clearTimeout(measureTimer);
                resolve(Math.floor(speed));
            }
        }
        xhr.open("GET", "https://bj.bcebos.com/mini-program/ui-wap-manage/baidu.png?id=" + Math.random(), true);
        xhr.send();
        xhr.onerror = () => {
            measureTimer && clearTimeout(measureTimer);
            measureTimerStatus = true;
            resolve(50)
        }
    });
}